import { Component, OnInit, Input } from '@angular/core';

import { AlertController, ActionSheetController } from '@ionic/angular';
import { ActionSheetService } from 'ng-zorro-antd-mobile';
import { HttpService } from 'src/app/shared/service/http.service';

import { AppGlobalService } from '../../shared/service/app-global.service';
@Component({
  selector: 'app-member-status',
  templateUrl: './member-status.component.html',
  styleUrls: ['./member-status.component.scss'],
})
export class MemberStatusComponent implements OnInit {

  @Input() tagData: any;

  constructor(
    private appGlobalService: AppGlobalService,
    private alert: AlertController,
    private actionSheet: ActionSheetService, // 必须有
    private http: HttpService,
    private actionSheetCtrl: ActionSheetController) { }

  ngOnInit() {
    // this.tagData.data.showType = 'home_view_vip_status';
    // console.log(this.tagData.data);
    // console.log(this.tagData.data.title);
    // console.log(this.tagData.data.title.iconName);
  }

  gotoDetail(obj) {
    if (obj.link) {
      const targetParams = {
        pageId: obj.link.target,
        params: obj.link.params,
        sessionId: localStorage.getItem('sessionId')
      };
      this.appGlobalService.goToForward(targetParams);
    } else {
      this.askAbout(obj.phone, obj.tips);
    }

  }

  async rightBut(butListLink) {
    const that = this;
    if (butListLink && butListLink.length > 0) {
      const butListLinkData = butListLink[0];
      const buttons: any = [];
      butListLinkData.formData.forEach((element, index) => {
        buttons.push(element.label ? element.label : element.call.label);
      });
      ActionSheetService.showActionSheetWithOptions(
        {
          options: buttons,
          maskClosable: true
        },
        buttonIndex => {
          if (buttonIndex > -1) {
            ActionSheetService.close();
            setTimeout(() => {
              that.selectItem(butListLink, buttonIndex);
            }, 200);
          }
        }
      );
    }
  }

  selectItem(item, index) {
    try {
      if (item && item.length > 0) {
        // tslint:disable-next-line:no-shadowed-variable
        const itemObj = item[0].formData[index];
        if (itemObj.link !== undefined) {
          const pageParams = {
            pageId: itemObj.link.target,
            params: itemObj.link.params,
            sessionId: localStorage.getItem('sessionId')
          };
          this.appGlobalService.goToForward(pageParams);
        } else if (itemObj.call !== undefined) {
          this.askAbout(itemObj.call.phone, itemObj.call.tips);
        }
      }
    } catch (error) {
      console.log('error----->' + JSON.stringify(error));
    }
  }



  async askPauseOrStop(manageServiceData) {
    const that = this;
    const buttonArray = [];
    if (manageServiceData && manageServiceData.length > 0) {
      const btnLen = manageServiceData.length;
      for (let i = 0; i < btnLen; i++) {
        buttonArray.push({
          text: '' + manageServiceData[i].label,
          cssClass: (i === btnLen - 1) ? 'as_font_color' : 'as_font_bd',
          handler: () => {
            that.gotoDetail(manageServiceData[i]);
          }
        });
      }
    }
    const actionSheet = await this.actionSheetCtrl.create({
      mode: 'ios',
      cssClass: 'as_padding as_common as_border_common',
      buttons: buttonArray
    });
    await actionSheet.present();
  }

  async askAbout(phone, tips) {
    const alert = await this.alert.create({
      message: tips,
      buttons: [
        {
          text: '取消',
          role: 'cancel',
          cssClass: 'secondary',
          handler: () => {
            console.log('Confirm Cancel');
          }
        }, {
          text: '拨打',
          cssClass: 'LJ',
          handler: () => {
            this.http.changeCallNumber(phone);
          }
        }
      ]
    });
    await alert.present();
  }

}
